package com.ht.web.emp;
import com.ht.bean.aduit.Assess;
import com.ht.bean.emp.Dep;
import com.ht.bean.emp.Empinfo;
import com.ht.bean.emp.Post;
import com.ht.bean.json.JsonData;
import com.ht.service.aduit.AssessService;
import com.ht.service.emp.DepService;
import com.ht.service.emp.EmpinfoService;
import com.ht.service.emp.PostService;
import com.ht.util.Contants;
import com.ht.util.Pager;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 员工管理
 *
 * */
@Controller
@RequestMapping("emp")
public class EmpController {
    @Resource
    private EmpinfoService empinfoService;
    @Resource
    private DepService depService;
    @Resource
    private PostService postService;
    @Resource
    private AssessService assessService;
    @Resource
    private JsonData jsonData;


    /**
     * @param empinfo
     * @return
     * 登录操作
     */
    @RequestMapping("login")
    @ResponseBody
    public Integer login(Empinfo empinfo, HttpSession session){
        Empinfo user = empinfoService.login(empinfo);
        if (user == null){
            return 1;
        }else if (user.getStatus() == Contants.STATUS_SW){
            return 2;
        }
        session.setAttribute("user",user);
        return 3;
    }

    /**
     * @param limit
     * @param page
     * @return
     * 获取员工资料
     */
    @RequestMapping("empdata")
    @ResponseBody
    public JsonData empdata(@Param("limit")int limit , @Param("page")int page,@Param("emp") Empinfo emp,Model model){
        //System.out.println("模糊数据"+emp);
        Pager pager = new Pager();
        pager.setPageSize(limit);
        pager.setCurrPage(page);
        jsonData.setCount(empinfoService.selprocount(emp));
        List<Empinfo> list = (empinfoService.allPageEmp(pager,emp));
        //根据部门id和职务id去找
        for (Empinfo empinfo : list){
            Dep dep = depService.selectByPrimaryKey(empinfo.getDep_id());
            Post post = postService.selectByPrimaryKey(empinfo.getPost_Id());
            //赋值
            empinfo.setDepname(dep.getDepname());
            empinfo.setPostname(post.getPos_Name());
        }
        jsonData.setData(list);
        return jsonData;
    }

    /**
     * @param
     * @return
     * 去到添加页面
     */
    @RequestMapping("toadd")
    public String toadd(Model model){
        List<Dep> deps = depService.getAllFdep();
        List<Post> posts = postService.list();
        model.addAttribute("deps",deps);
        model.addAttribute("posts",posts);
        return "emp/empadd";
    }

    /**
     * @param id
     * @return
     * 二级联动
     */
    @RequestMapping("selectpost")
    @ResponseBody
    public List<Dep> selectpost(Integer id){
        if (id == null){
            return null;
        }
        List<Dep> deps2 = depService.getDepByid(id);
        return deps2;
    }

    /**
     * @param empinfo
     * @return
     * 添加
     */
    @RequestMapping("add")
    @ResponseBody
    public Boolean add(Empinfo empinfo,String province,String city,String county,String detail,Integer fdepid,Integer zdepid) {
        if (zdepid == null || zdepid == 0){
            empinfo.setDep_id(fdepid);
        }else {
            empinfo.setDep_id(zdepid);
        }
        String dz = province+"/"+city+"/"+county+"/"+detail;
        empinfo.setAddress(dz);
        Empinfo cf = empinfoService.getEmpByName(empinfo);
        if (cf == null){
            empinfo.setPassword(Contants.PASSWORD_TA);//默认密码
            empinfo.setStatus(Contants.STATUS_SU);//默认开启
            empinfoService.insert(empinfo);
            int id = empinfoService.getId();
            Assess assess = new Assess();
            assess.setEmpid(id);
            assessService.insert(assess);

            //如果是校长顺便添加成部门负责人
            if (empinfo.getPost_Id() == 1 || empinfo.getPost_Id() == 2){
                //校长或主任
                Dep dep = depService.selectByPrimaryKey(empinfo.getDep_id());
                dep.setChairman(empinfo.getEmp_id());
                depService.updateByPrimaryKeySelective(dep);
            }
            return true;
        }
        return false;
    }

    /**
     * @param id
     * @return
     * 删除
     */
    @RequestMapping("del")
    @ResponseBody
    public Boolean del(@Param("id") Integer id){
        int b = empinfoService.deleteByPrimaryKey(id);
        if (b == 0){
            return false;
        }
        return true;
    }

    /**
     * @param empinfo
     * @return
     * 修改
     */
    @RequestMapping("update")
    @ResponseBody
    public Boolean updata(Empinfo empinfo,Integer fdepid,Integer zdepid,String province,String city,String county,String detail){
        //如果是一级部门
        if (zdepid == null || zdepid == 0){
            empinfo.setDep_id(fdepid);
        }else {
            empinfo.setDep_id(zdepid);
        }
        Empinfo empinfo1 = empinfoService.getEmpByName(empinfo);//根据号码找；现在的
        Empinfo empinfo2 = empinfoService.selectByPrimaryKey(empinfo.getEmp_id());//根据id找
        //是否有这个号码且如果有这个人
        if (empinfo1 == null || empinfo2.getPhone().equals(empinfo.getPhone())){
            //拼接地址
            String dz = province+"/"+city+"/"+county+"/"+detail;
            empinfo.setAddress(dz);
            empinfoService.updateByPrimaryKeySelective(empinfo);

            //如果是校长顺便添加成部门负责人
            if (empinfo.getPost_Id() == 1 || empinfo.getPost_Id() == 2){
                //校长或主任
                Dep dep = depService.selectByPrimaryKey(empinfo.getDep_id());
                dep.setChairman(empinfo.getEmp_id());
                depService.updateByPrimaryKeySelective(dep);
            }
            return true;
        }
        return false;
    }

    /**
     * @param empinfo
     * @return
     * 修改状态
     */
    @RequestMapping("set")
    @ResponseBody
    public Boolean set(Empinfo empinfo){
        if (empinfo.getStatus() == Contants.STATUS_SU){
            empinfo.setStatus(Contants.STATUS_SW);
        }else {
            empinfo.setStatus(Contants.STATUS_SU);
        }
        empinfoService.updateByPrimaryKeySelective(empinfo);
        return true;
    }

    /**
     * @param empinfo
     * @return
     * 重置密码
     */
    @RequestMapping("reset")
    @ResponseBody
    public Boolean reset(Empinfo empinfo){
        if (Contants.PASSWORD_TA.equals(empinfo.getPassword())){
           return false;
        }
        empinfo.setPassword(Contants.PASSWORD_TA);
        empinfoService.updateByPrimaryKeySelective(empinfo);
        return true;
    }

    /**
     * @param empinfo
     * @return
     * 编辑
     */
    @RequestMapping("toedit")
    public String edit(Empinfo empinfo,Model model){
        Empinfo empinfo1 = empinfoService.selectByPrimaryKey(empinfo.getEmp_id());//当前用户
        //获取部门
        Dep dep = depService.selectByPrimaryKey(empinfo1.getDep_id()); //证据用户找父级
        List<Dep> fdeps = depService.getAllFdep();//所有父级
        if(dep.getParentid() != 0){//判断当前部门是不是一级部门
            dep = depService.getDepByFid(dep.getParentid());//获取父级对象
        }
        model.addAttribute("fdeps",fdeps);//所有父级
        model.addAttribute("fdep",dep);//当前父级
        List<Dep> dep1 = depService.getDepByid(dep.getDepid());
        model.addAttribute("deps",dep1);
        //获取职位
        List<Post> posts = postService.list();
        model.addAttribute("posts",posts);
        model.addAttribute("emp",empinfo1);
        //获取地址
        String[] strings = empinfo1.getAddress().split("/");//前台参数，截取省 市 区
        model.addAttribute("province",strings[0]);//省
        model.addAttribute("city",strings[1]);//市
        model.addAttribute("county",strings[2]);//区
        model.addAttribute("detailed",strings[3]);//详细地址
        return "emp/empedit";
    }

    //职位修改
    @RequestMapping("postedit")
    @ResponseBody
    public Integer postedit(Integer id,Integer fdep,Integer dep,Empinfo empinfo){
        if (id == null){
            return 0;
        }else if (dep == null){
            //父级
            if (id==2 || id==3 || id==4 || id==5 || id==6 || id==7){
                return 1;
            }else {
                empinfo.setDep_id(fdep);
                empinfo.setPost_Id(id);
                Empinfo result = empinfoService.selectcrux(empinfo);
                if (result != null){
                    return 2;
                }
            }
        }else {
            //子级
            if (id == 1){
                return 3;
            }else if (id == 2){
                empinfo.setDep_id(dep);
                empinfo.setPost_Id(id);
                Empinfo result = empinfoService.selectcrux(empinfo);
                if (result != null){
                    return 4;
                }
            }
        }
        return 0;
    }
}

